<?php
class AdminAction extends Action {
    //后台首页
	public function index(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		else
		{
			$this->assign('menu',$this->menu());
			$this->assign('menu_n',$this->menu_n());
			$this->assign("time",time());
			$this->display();
		}
	}
	
	//后台menu
	public function menu_n(){
		return array(
			'会员管理'		=>	'member',
			'管理员信息'		=>	'admininfo',
			'全局配置管理'	=>	'config',
			'资源信息管理'	=>	'resource',
			'评论管理'	=>	'comment',
			'会员级别管理'	=>	'level',
			'资源库管理'		=>	'menu',
			//'资源库目录管理'	=>	'category',
			'资源回收站'		=>	'recover',
			'专题管理'		=>	'topic',
			'版务管理'		=>	'confart',
			'广告管理'		=>	'ad',
			//'系统图片管理'	=> 	'confimg',
			'网站统计'      =>   'tj' ,
			'焦点图'      =>   'jd',
			'共享发布'		=>	'share'
		);
	}
	
	public function menu()
	{
		$menu = array(
			'管理员信息'		=>
				array(
					'信息管理'	=>U('Admininfo/index'),
					'修改密码'	=>U('Admininfo/editpassword'),
				),
			'会员管理'		=> U('Admin/member'),
			//'资源库管理'		=>U('Menu/index'),
			'资源目录管理'	=> 
				array(
					'图片'	=> U('Category/picture'),
					'视频'	=> U('Category/video'),
					'动画'	=> U('Category/flash'),
					'游戏'	=> U('Category/game'),
					'文章'	=> U('Category/article'),
					'挂图'	=> U('Category/image'),
					'图书'	=> U('Category/book'),
					'展品'	=> U('Category/exhibits'),
					'知识题'	=> U('Category/knowledge'),
				),
			'全局配置管理'	=>
				array(
					'网站配置'	=>U('Config/index'),
					'关键词设置'	=>U('Keyword/index'),
				),
			'资源信息管理'	=> 
				array(
					'图片'	=> U('Resource/picture'),
					'视频'	=> U('Resource/video'),
					'动画'	=> U('Resource/flash'),
					'游戏'	=> U('Resource/game'),
					'文章'	=> U('Resource/article'),
					'挂图'	=> U('Resource/image'),
					'图书'	=> U('Resource/book'),
					'展品'	=> U('Resource/exhibits'),
					'知识题'	=> U('Resource/knowledge'),
				),
			'专题管理'		=> U('Topic/index'),
			'版务管理'		=> U('Confart/index'),
			//'系统图片管理'	=> U('Confimg/index'),
			'评论管理'		=> U('Comment/index'),
			'级别管理'		=> U('Level/index'),
			'广告管理'		=> U('Ad/index'),
			'资源回收站'	=> 
				array(
					'图片'	=> U('Recover/picture'),
					'视频'	=> U('Recover/video'),
					'动画'	=> U('Recover/flash'),
					'游戏'	=> U('Recover/game'),
					'文章'	=> U('Recover/article'),
					'挂图'	=> U('Recover/image'),
					'图书'	=> U('Recover/book'),
					'展品'	=> U('Recover/exhibits'),
					'知识题'	=> U('Recover/knowledge'),
				),
				
			'网站统计'	=> 
				array(
					//'访问量'	=> U('Tongji/fangwen'),
					'注册量'	=> U('Tongji/zhuce'),
					'内容添加量'	=> U('Tongji/neirong'),
					//'下载量'	=> U('Tongji/xiazai'),
				),
			'焦点图'	=> 
				array(
					'首页焦点图'	=> U('Jiaodian/shouye'),
					'图片焦点图'	=> U('Jiaodian/picture'),
					'文章焦点图'	=> U('Jiaodian/article'),
				),
			'共享发布'	=>
				array(
					'图片类'	=> U('Share/pictures'),
					'视频类'	=> U('Share/videos'),
					'文档类'	=> U('Share/articles'),
				),
		);
		return $menu;
	}
	
	//会员管理
	public function member()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$user = M('User');
		$level=M('Level');
		//if($_POST){
			$key=$_GET;
			if($key['lmins']&&$key['lmaxs']){
				$map['_string']="(`score` >= '".$key['lmins']."') and (`score` <= '".$key['lmaxs']."')";
			}else{
				if($key['lmins']){
		  			$map['score']=array('egt',$key['lmins']);
		  		}
		  		if($key['lmaxs']){
		  			$map['score']=array('elt',$key['lmaxs']);
		  		}
			}
			if($key['username']){
				$map['username'] = $key['username'];
			}
			if($key['lid']){
				$this->assign('lid',$key['lid']);
			}
			$this->assign('searkey',$key);
	//	}
		$map['utype'] = 0; 
		$count = $user->where($map)->count();
		$page = new Page($count,15);
		$list = $user->where($map)->limit($page->firstRow.",".$page->listRows)->order('regtime desc')->select();
		$name=$level->field('lid,lname')->select();
		if($list){
			$sta = 1;
		}else{
			$sta = 0;
		}
		$this->assign('sta',$sta);
		$this->assign('name',$name);
		$this->assign("list",$list);
		$this->assign("page",$page->show());
	    $this->display();
	}
	
	//添加会员
	public function adduser()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
		{
			$cond = $_POST;
			$cond['birth'] = $_POST['year']."年".$_POST['moth']."月".$_POST['day']."日";
			$cond['_column'] = array('username','password','email','address','birth','nickname','sex','email','tel');
			foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
			$user = D('User');
			if($user->create($data))
			{
				if(($uid = $user->add()) != false)
				{
					$this->redirect('Admin/member');
				}
			}else
			{
				$this->assign('erorsta','1');
				$this->assign('error',$user->getError());
			}
		}
		$this->display();
	}
	
	//编辑会员
	public function edituser()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$uid = $_GET['uid'];
		if($uid){
			if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
			{
				$user = D("User");
				$data = array(
					"uid"=>$_GET['uid'],
					"username"=>$_POST['username'],
					"sex"=>$_POST['sex'],
					"email"=>$_POST['email'],
					"nickname"=>$_POST['nickname'],
					"score"=>$_POST['score'],
					"tel"=>$_POST['tel'],
				);
				if(!empty($_POST['password'])&& ($_POST['password']==$_POST['repassword'])){
					$data['password'] = md5($_POST['password']);
				}else{
					if(!empty($_POST['password'])){
						$this->assign('erorsta','1');
						$this->assign('error','密码输入不正确');
						$map['uid'] = $uid;
						$info = $user->where($map)->find();
						$this->assign('info',$info);
						$this->assign('uid',$uid);
						$this->display();
						exit;
					}
				}
				if($user->create($data)){
					if($user->save()){
						$this->success('编辑成功',__URL__.'/member/');
					}else{
						$this->success('无编辑',__URL__.'/member/');
					}
				}else{
					$this->assign('erorsta','1');
					$this->assign('error',$user->getError());
					$map['uid'] = $uid;
					$info = $user->where($map)->find();
					$this->assign('info',$info);
					$this->assign('uid',$uid);
					$this->display();
				}
			}else{
				$user = M('User');
				$map['uid'] = $uid;
				$info = $user->where($map)->find();
				$this->assign('info',$info);
				$this->assign('uid',$uid);
				$this->display();
			}
		}else{
			$this->redirect(Admin/member);
		}
	}
	
	//会员删除
	public function dele(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$uid = $_POST['uid'];
		if($uid)
		{
			$map['uid'] = $uid;
			$res = M('User')->where($map)->delete();
			if($res) echo 1;
			else echo 0;
		}
		else echo 0;
	}
	
	//禁用会员
	public function forbidden()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$uid = $_POST['uid'];
		
		if($uid)
		{
			$map['uid'] = $uid;
			$map["status"] = 1;
			$data["status"] = 2;
			$res = M('User')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
	
	//启用用户
	public function enable()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$uid = $_POST['uid'];
		
		if($uid)
		{
			$map['uid'] = $uid;
			//$map["status"] = 0;
			$data["status"] = 1;
			$res = M('User')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
	
	
	public function search(){
	  	$model=M('Level');
	  	$lid=$_POST['lid'];
	  	if($lid){
		  	$map=array('lid'=>$lid);
		  	$var=$model->where($map)->find();
		  	if($var){
		  		$js=json_encode($var);
				echo $js;
		  	}else echo 0;
	  	}else echo 0;
	}
}